Introduction to GRASS GIS

Basic notions, interfaces and temporal framework

Author

Veronica Andreo

Published

March 30, 2023

GRASS GIS general stuff

  • GRASS GIS (Geographic Resources Analysis Support System), a FOSS suite used for geospatial data management and analysis, image processing, graphics and maps, spatial modeling, and visualization.
  • Originally developed by the U.S. Army Construction Engineering Research Laboratories for land management and environmental planning (1982-1995).
  • More history: https://grass.osgeo.org/about/history/
  • Free and open source, you can use, modify, improve, share
  • Strong user community, commercial support
  • Large amount of tools: 500+ core modules, 300+ addons
  • GUI and CLI interfaces
  • Python API and libraries - new grass.jupyter library can be tried online
  • Connection with R, QGIS, WPS, etc.
  • Different data types supported: raster (including satellite imagery), 3D raster or voxel, vector and space-time datasets

Open GRASS for the first time

As of version 8.0, GRASS has modified its startup to make it more user friendly:
First time launching GRASS 8

From the Data catalog tab you can manage several actions and if you do not yet have imported data into the GRASS database, the software creates the directory structure or database automatically.

Database

  • GRASS database (directory with projects): When running GRASS GIS for the first time, a folder named “grassdata” is automatically created. Depending on the operating system, it can be found in $HOME (*nix) or My Documents (MS Windows).
  • Location (a project): A location is defined by its coordinate reference system (CRS). The location that is automatically created is in WGS84 (EPSG:4326). If you have data in another CRS, you should ideally create a new location.
  • Mapset (a subproject): Each location can have many mapsets to manage different aspects or sub-regions of a project. When creating a new location, GRASS GIS automatically creates a special mapset called PERMANENT where the central data of the project (e.g., base maps, road network, dem, etc.) can be stored.

GRASS GIS database

Note

More info: https://grass.osgeo.org/grass-stable/manuals/grass_database.html.

Computational region

Another fundamental concept of GRASS GIS (and very useful when working with raster data) is that of the computational region. It refers to the boundary configuration of the analysis area and spatial resolution (raster). The computational region can be defined and modified with the command g.region to the extent of a vector map, a raster or manually to some area of interest. The output raster maps will have an extent and spatial resolution equal to the computational region, while vector maps are always processed at their original extent.

Computational region

Note

For more details, see the wiki on Computational Region.

Modules and extensions

GRASS has more than 500 modules for the most varied tasks:

Prefix Function class Type of command Example
g.* general general data management g.rename: renames map
d.* display graphical output d.rast: display raster map
r.* raster raster processing r.mapcalc: map algebra
v.* vector vector processing v.clean: topological cleaning
i.* imagery imagery processing i.pca: Principal Components Analysis on imagery group
r3.* voxel 3D raster processing r3.stats: voxel statistics
db.* database database management db.select: select value(s) from table
ps.* postscript PostScript map creation ps.map: PostScript map creation
t.* temporal space-time datasets t.rast.aggregate: raster time series aggregation

Extensions or add-ons can be installed from the central GitHub repository or from other users’ GitHub (or similar repositories) using the command g.extension. For example:

 # install an extension from the GRASS GIS repository
 g.extension extension=r.hants
 
 # install an extension from another GitHub repository
 g.extension extension=r.change.stats \
   url=https://github.com/mundialis/r.change.stats

Graphical User Interface (GUI)

Command line

GRASS + Python

Python package grass.script

The grass.script or GRASS GIS Python Scripting Library provides functions for calling GRASS modules within Python scripts. The most commonly used functions include:

  • run_command: used when the output of the modules is a raster or vector, no text type output is expected
  • read_command: used when the output of the modules is of text type
  • parse_command: used with modules whose output can be converted to key=value pairs
  • write_command: used with modules that expect text input, either in the form of a file or from stdin

It also provides several wrapper functions for frequently used modules, for example:

  • To get info from a raster, script.raster.raster_info() is used: gs.raster_info('dsm')
  • To get info of a vector, script.vector.vector_info() is used: gs.vector_info('roads')
  • To list the raster in a location, script.core.list_grouped() is used: gs.list_grouped(type=['raster'])
  • To obtain the computational region, script.core.region() is used: gs.region()
Note

More info: https://grass.osgeo.org/grass-stable/manuals/libpython/script_intro.html

Python package grass.jupyter

The grass.jupyter library improves the integration of GRASS and Jupyter, and provides different classes to facilitate GRASS maps visualization:

  • init: starts a GRASS session and sets up all necessary environment variables
  • Map: 2D rendering
  • Map3D: 3D rendering
  • InteractiveMap: interactive visualization with folium
  • TimeSeriesMap: visualization for spatio-temporal data
Note

More info: https://grass.osgeo.org/grass-stable/manuals/libpython/grass.jupyter.html

GRASS + R through rgrass package

We can use R within a GRASS GIS session or use GRASS GIS within an R session

Demo session overview

Habitat suitability mapping for the Asian tiger mosquito in Northern Italy based on variables derived from daily LST data

  • GRASS
    • Import species records
    • Create random background points
    • Create different environmental layers from daily LST data
  • R
    • Read data from GRASS into R
    • Model species distribution
    • Model evaluation
    • Visualization of results

Jarnevich et al. 2015. doi:10.1016/j.ecoinf.2015.06.007

Data for the session

  • Records of Aedes albopictus (Asian tiger mosquito) in Northern Italy downloaded from GBIF
  • Daily MODIS LST reconstructed by mundialis based on Metz et al. 2017
    • 1 km spatial resolution
    • Converted to Celsius degrees


Get sample location, mosquito records and code

The grassdata_ogh folder’s tree should look like this:

  grassdata_ogh/
  └── eu_laea
      ├── italy_LST_daily
      └── PERMANENT

MASK

  • Masks are set with r.mask or creating a raster map called MASK.
  • Masks are virtual masks, they are only actually applied when reading raster maps
  • All cells that are NULL in the MASK map will be ignored (also all areas outside the computational region).
  • Vector maps can be also used as masks

a- Elevation raster and lakes vector maps. b- Only the raster data inside the masked area are used for further analysis. c- Inverse mask.

MASK examples

# use vector as mask
r.mask vector=lakes

# use vector as mask, set inverse mask
r.mask -i vector=lakes

# mask categories of a raster map
r.mask raster=landclass96 maskcats="5 thru 7"

# create a raster named MASK
r.mapcalc expression="MASK = if(elevation < 100, 1, null())"

# remove mask
r.mask -r

Masks are only actually applied when reading a GRASS raster map, i.e., when used as input.

GRASS GIS HELP!!!

  • g.manual: in the main GUI under Help or just pressing F1
  • --help or --h flag after the module name in the terminal
  • GRASS website: rich learn section with links to videos, tutorials, courses, books, etc.
  • GRASS wiki: examples, explanations on particular modules or tasks, tutorials, applications, etc.
  • grass-user mailing list: subscribe and post or check the archives
  • Link to source code and history in each module manual page, eg., t.rast.algebra